#include "efr.h"
#include "eeprom.h"

#if (CONFIG_IC_TYPE == CHIP_SS881X)
void eeprom_write(uint8_t addr, uint8_t dat)
{
    IRQ_DISABLE();

    WRITE_EFR_REG(EEPADR, addr);        /* write eeprom address */
    WRITE_EFR_REG(EEPDAT, dat);         /* write data */
    WRITE_EFR_REG(EEPCON, 0x0A);        /* enable wr */
    /* write WR complete */
    while (READ_EFR_REG(EEPCON) & (1 << 3))
        WDOG_KEEP_ALIVE();
    WRITE_EFR_REG(EEPCON, 0x00);

    IRQ_ENABLE();
}

uint8_t eeprom_read(uint8_t addr)
{
    uint8_t eeprom_val;
    IRQ_DISABLE();
    
    WRITE_EFR_REG(EEPADR, addr);        /* write eeprom address */
    WRITE_EFR_REG(EEPCON, 0x05);        /* enable rd */
    /* write rd complete */
    while (READ_EFR_REG(EEPCON) & (1 << 2))
        WDOG_KEEP_ALIVE();
    eeprom_val = READ_EFR_REG(EEPDAT);
    WRITE_EFR_REG(EEPCON, 0x00);

    IRQ_ENABLE();
    
    return eeprom_val;
}
#endif